﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace BACKOFFICE
{
    public partial class Startup : DevExpress.XtraEditors.XtraForm
    {
        Thread StartupProcedures;

        public Startup()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void Startup_Load(object sender, EventArgs e)
        {
            progressPanel1.Description = "";

            StartupProcedures = new Thread(new ThreadStart(Startup1));
            StartupProcedures.Name = "STARTUP THREAD";
            StartupProcedures.Start();
        }

        void Startup1()
        {
            try
            {
                progressPanel1.Caption = "Getting Startup Information";
                Thread.Sleep(250);
                //try
                //{
                //    StaticProperties.StartupStuff.FileLocation = Environment.CurrentDirectory;

                //    if (File.Exists(StaticProperties.StartupStuff.FileLocation + @"\" + StaticProperties.StartupStuff.FileName ))
                //    {

                //        XmlDocument xmldoc = new XmlDocument(); 
                //        XmlNodeList xmlnode; 
                //        int i = 0; 
                //        string str = null;
                //        FileStream fs = new FileStream("Startup.xml", FileMode.Open, FileAccess.Read); 
                //        xmldoc.Load(fs);
                //        xmlnode = xmldoc.GetElementsByTagName("Startup"); 
                //        for (i = 0; i <= xmlnode.Count - 1; i++) 
                //        { 
                //            xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                //            str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                //            DataTableClass.SQLServerIP = (str);

                //            str = xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
                //            DataTableClass.SQLServerPORT = (str);
                //        }

                //        DataTableClass.myConnectionString = DataTableClass.connectionstring();
                //    }
                //    else 
                //    {
                //SQLServerInfo addinfo = new SQLServerInfo();
                //addinfo.ShowDialog();

                //if(addinfo.DialogResult == System.Windows.Forms.DialogResult.OK)
                //{
                DataTableClass.SQLServerIP = "192.168.1.13";// (SQLServerInfo._main.lblserverIP.Text);
                DataTableClass.SQLServerPORT = "1433";// (SQLServerInfo._main.lblServerPORT.Text);

                            DataTableClass.myConnectionString = DataTableClass.connectionstring();
                        //}
                //    }
                //}
                //catch(Exception E)
                //{
                //    MessageBox.Show(E.Message);
                //}

                progressPanel1.Caption = "Getting Network Information";
                Thread.Sleep(1000);
                StaticProperties.StartupStuff.theRunningProc = new Process();
                StaticProperties.StartupStuff.theRunningProc.StartInfo = new ProcessStartInfo();
                StaticProperties.StartupStuff.theRunningProc.StartInfo.FileName = @"C:\Users\Roger Jenkins\Downloads\Open.NAT-master1\Open.NAT-master\Open.Nat.ConsoleTest\bin\Debug\Open.Nat.ConsoleTest.exe";
                StaticProperties.StartupStuff.theRunningProc.StartInfo.CreateNoWindow = true;
                //StaticProperties.StartupStuff.theRunningProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //StaticProperties.StartupStuff.theRunningProc.Start();

                StaticProperties.LocalIPAddress = StaticProperties.GetLocalIP();
                Thread.Sleep(500);

                if (DataTableClass.IsServerConnected())
                {
                    progressPanel1.Caption = "Loading Employee Data";
                    Thread.Sleep(125);
                    DataTableClass.LoadDataTable("EmployeeLogin", DataTableClass.EmployeeLogin);
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("EmployeeInfo", DataTableClass.EmployeeInfo);
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("EmployeeTimeSheet", DataTableClass.EmployeeTimeSheet);
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("JobTitles", DataTableClass.JobTitles);
                    //Thread.Sleep(250);

                    progressPanel1.Caption = "Loading Menu Items";
                    Thread.Sleep(125);
                    DataTableClass.LoadDataTable("MenuCategory", DataTableClass.MenuItemsCategory);
                    DataTableClass.LoadMenuCategoryDictionary();
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("MenuItems", DataTableClass.MenuItems);
                    DataTableClass.LoadMenuItemsDictionary();
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("ModificationList", DataTableClass.ModificationList);
                    //Thread.Sleep(250);


                    progressPanel1.Caption = "Loading Receipt Data";
                    Thread.Sleep(125);
                    DataTableClass.LoadDataTable("Receipt", DataTableClass.Receipt);
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("ReceiptData", DataTableClass.ReceiptData);
                    //Thread.Sleep(250);
                    DataTableClass.LoadDataTable("PaymentTypes", DataTableClass.PaymentTypes);
                    //Thread.Sleep(250);
                    StaticProperties.StartingReceiptNo = 100;
                    //MessageBox.Show(StaticProperties.ReceiptNoGenerator());


                    progressPanel1.Caption = "Loading Table Chart";
                    Thread.Sleep(1000);
                    //MessageBox.Show("DONE");
                }
                else
                {
                    throw new ArgumentException("Not able to connect to Database....");
                }
            }
            catch(Exception E)
            {
                MessageBox.Show(E.Message);
            }
            finally
            {
                this.Close();
            }
        }
    }
}